//////////////////////////////////////////////
// File				:	InputDevice.cpp
//
// Author			:	Nick Faletra (NF)
//
// Date Created		:	12/20/2013
//////////////////////////////////////////////
#include "InputDevice.h"
#include "../Utility/Utility.h"

CInputDevice::CInputDevice() : m_pDevice(nullptr);
{
	ClearBufferedData();
}

CInputDevice::~CInputDevice()
{
	if (m_pDevice)
	{
		m_pDevice->Unacquire();
		SAFE_RELEASE(m_pDevice);
	}
}

void CInputDevice::ClearBufferedData()
{
	memset(&m_didod, 0, sizeof(DIDEVICEOBJECTDATA));
	m_dwBufferElements = BUFFER_SIZE;
}

bool CInputDevice::ReadBufferedDevice()
{
	if (!m_pDevice)
		return false;

	if (FAILED(m_pDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), m_didod, &m_dwBufferElements, 0)))
	{
		if (FAILED(m_pDevice->Acquire()))
			return false;
		
		ClearBufferedData();

		if (FAILED(m_pDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), m_didod, &m_dwBufferElements, 0)))
			return false;
	}

	return true;
}

bool CInputDevice::Acquire()
{
	return m_pDevice ? SUCCEEDED(m_pDevice->Acquire()) : false;
}

bool CInputDevice::Unacquire()
{
	return m_pDevice ? SUCCEEDED(m_pDevice->Unacquire()) : false;
}